//
//  RACommandViewController.m
//  RACExample
//
//  Created by 山刘 on 2020/5/29.
//  Copyright © 2020 山刘. All rights reserved.
//

#import "RACommandViewController.h"

#pragma mark - ====================M层====================
#pragma mark - ====================VM层====================
@interface RACommandViewModel : NSObject
@property (nonatomic, strong) RACCommand *requestCommand;
@end
@implementation RACommandViewModel
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}
- (void)racInit {
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [[RACScheduler mainThreadScheduler] afterDelay:1 schedule:^{
                [subscriber sendNext:input];
                [subscriber sendCompleted];
            }];
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    [[_requestCommand.executionSignals switchToLatest] subscribeNext:^(id  _Nullable x) {
        NSLog(@"收到信号(switchToLatest)：%@",x);
    }];
    [_requestCommand.errors subscribeNext:^(NSError * _Nullable x) {
        NSLog(@"收到信号(errors)：%@",x.localizedDescription);
    }];
    [[[_requestCommand.executing skip:1] take:1] subscribeNext:^(NSNumber * _Nullable x) {
        if([x boolValue] == YES){
            NSLog(@"RACCommand命令正在执行...");
        }else{
            NSLog(@"RACCommand命令不在执行中！！！");
        }
    }];
}
@end
#pragma mark - ====================V层====================

@interface RACommandViewController ()
@property(nonatomic, strong) RACommandViewModel *viewModel;
@end

@implementation RACommandViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self segInitViews];
    [self bindViewModel];
}
#pragma mark - Init Views
-(void)segInitViews{}

#pragma mark - Layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
}

#pragma mark - Private Methods

#pragma mark - RAC Data Binding
- (void)bindViewModel {
    [self.viewModel.requestCommand execute:@1];
}

#pragma mark - getter
-(RACommandViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[RACommandViewModel alloc]init];
    }
    return _viewModel;
}
@end
